#参考了网上的部分代码，自己参考其思路重新构思且加了些改动
animals=["金钱豹", "虎","长颈鹿", "斑马", "鸵鸟", "企鹅", "信天翁"]
features = ["有奶", "有毛发", "有羽毛", "会飞", "会下蛋",
            "吃肉", "有犬齿", "有爪","眼盯前方", "有蹄", "嚼反刍",
            "黄褐色", "身上有暗斑点", "身上有黑色条纹", "有长脖子",
            "有长腿", "不会飞", "会游泳", "有黑白二色", "善飞",
            "哺乳动物", "鸟", "食肉动物", "蹄类动物", "金钱豹", "虎",
            "长颈鹿", "斑马", "鸵鸟", "企鹅", "信天翁"]

#规则库
rule1 = [2]                            # if 动物有毛发2  then  动物是哺乳动物21
rule2 = [1]                            # if 动物有奶1  then  动物是哺乳动物21
rule3 = [3]                            # if 动物有羽毛3  then  动物是鸟22
rule4 = [4, 5]                         # if 动物会飞4 and  会生蛋5 then  动物是鸟22
rule5 = [6]                            # if 动物吃肉6 then 动物是食肉动物23
rule6 = [7, 8, 9]                      # if 动物有犀利牙齿7 and 有爪8 and 眼向前方9 then 动物是食肉动物23
rule7 = [21, 10]                       # if 动物是哺乳动物21 and 有蹄10 then 动物是有蹄类动物24
rule8 = [21, 11]                       # if 动物是哺乳动物21 and 反刍11 then 动物是有蹄类动物24
rule9 = [21, 23, 12, 13]               # if 动物是哺乳动物21 and 是食肉动物23 and 有黄褐色12 and 有暗斑点13 then 动物是豹25
rule10 = [21, 23, 12, 14]              # if 动物是哺乳动物21 and 是食肉动物23 and 有黄褐色12 and 有黑色条纹14 then 动物是虎26
rule11 = [24, 15, 16, 13]              # if动物是有蹄类动物24  and 有长脖子15 and 有长腿16 and 有暗斑点13 then 动物是长颈鹿27
rule12 = [24, 14]                      # if 动物是有蹄类动物24 and 有黑色条纹14 then 动物是斑马28
rule13 = [22, 17, 16, 15, 19]          # if 动物是鸟22 and 不会飞17 and 有长脖子16 and 有长腿15 and 有黑白二色15 then 动物是鸵鸟29
rule14 = [22, 17, 18, 19]              # if 动物是鸟22 and 不会飞17 and 会游泳18 and 有黑白二色19 then  动物是企鹅30
rule15 = [22, 4, 5]                    # if 动物是鸟22 and善飞4 and 会生蛋5 then 动物是信天翁31

print('***************************************************知识库**************************************************')
i = 0
while i < 24:
    print('\t%d' %(i+1) +'.'+ features[i], end='\t')
    i = i+1
    if i % 6 == 0:
        print('\n')

print('***************************************************可识别动物**************************************************')
while i < 31:
    print('\t%d' %(i+1) +'.'+ features[i]+ '  ', end='\t')
    i = i+1
flag= int(input('请选择\n1:正向推理\n2:反向推理\n'))

print('***************************************************************************************************************')
print('***************************************************下面开始推理**************************************************')
if flag==1:#正向推理
    choose = input('请输入选择的特征数字（用空格隔开，回车结束输入）：')
    choose = list(choose.split())
    fact = [int(x) for x in choose]
    print('*****************开始推理*********************')
    while(1):
        if set(rule1) <= set(fact):  # 检查rule1对应的集合是否是fact对应的集合的子集。
            print('if 动物有毛发2  then  动物是哺乳动物21')
            fact.append(21)
            print("更新后的数据库为：", fact)
            print("")
        if set(rule2) <= set(fact):
            print('if 动物有奶1  then  动物是哺乳动物21')
            fact.append(21)
            print("更新后的数据库为：", fact)
            print("")
        if set(rule3) <= set(fact):
            print('if 动物有羽毛3  then  动物是鸟22')
            fact.append(22)
            print("更新后的数据库为：", fact)
            print("")
        if set(rule4) <= set(fact):
            print('if 动物会飞4 and  会生蛋5 then  动物是鸟22')
            fact.append(22)
            print("更新后的数据库为：", fact)
            print("")
        if set(rule5) <= set(fact):
            print('if 动物吃肉6 then 动物是食肉动物23')
            fact.append(23)
            print("更新后的数据库为：", fact)
            print("")
        if set(rule6) <= set(fact):
            print('if 动物有犀利牙齿7 and 有爪8 and 眼向前方9 then 动物是食肉动物23')
            fact.append(23)
            print("更新后的数据库为：", fact)
            print("")
        if set(rule7) <= set(fact):
            print('if 动物是哺乳动物221 and 有10蹄 then 动物是有蹄类动物')
            fact.append(24)
            print("更新后的数据库为：", fact)
            print("")
        if set(rule8) <= set(fact):
            print('if 动物是哺乳动物21 and 反刍11 then 动物是有蹄类动物')
            fact.append(24)
            print("更新后的数据库为：", fact)
            print("")
        if set(rule9) <= set(fact):
            print("if 动物是哺乳动物21 and 是食肉动物23 and 有黄褐色12 and 有暗斑点13 then 动物是豹25")
            print("推理结果为：", end=" ")
            print(animals[0])
            break
        elif set(rule10) <= set(fact):
            print("if 动物是哺乳动物21 and 是食肉动物23 and 有黄褐色12 and 有黑色条纹14 then 动物是虎26")
            print("推理结果为：", end=" ")
            print(animals[1])
            break
        elif set(rule11) <= set(fact):
            print("if动物是有蹄类动物24  and 有长脖子15 and 有长腿16 and 有暗斑点13 then 动物是长颈鹿27")
            print("推理结果为：", end=" ")
            print(animals[2])
            break
        elif set(rule12) <= set(fact):
            print("if 动物是有蹄类动物24 and 有黑色条纹14 then 动物是斑马28")
            print("推理结果为：", end=" ")
            print(animals[3])
            break
        elif set(rule13) <= set(fact):
            print("if 动物是鸟22 and 不会飞17 and 有长脖子16 and 有长腿15 and 有黑白二色15 then 动物是鸵鸟29")
            print("推理结果为：", end=" ")
            print(animals[4])
            break
        elif set(rule14) <= set(fact):
            print("if 动物是鸟22 and 不会飞17 and 会游泳18 and 有黑白二色19 then  动物是企鹅30")
            print("推理结果为：", end=" ")
            print(animals[5])
            break
        elif set(rule15) <= set(fact):
            print("if 动物是鸟22 and善飞4 and 会生蛋5 then 动物是信天翁31")
            print("推理结果为：", end=" ")
            print(animals[6])
            break
        else:
            print('识别失败！')
            break
elif flag == 2:
        animal = int(input('请选择动物的种类编号，回车结束输入：'))
        answer = input('\n请选择动物的特征编号，用空格隔开，回车结束输入：')
        fact=[]
        fact.append(animal)
        answer = list(answer.split())
        new_answer = [int(x) for x in answer]
        print("事实库：", new_answer)
        print("逆向推理过程如下：")
        real = []
        if animal == 25:
            print(features[animal - 1],
                  '\nif 动物是哺乳动物21 and 是食肉动物23 and 有黄褐色12 and 有暗斑点13 then 动物是豹')
            real = rule9
        elif animal == 26:
            print(features[animal - 1],
                  '\nif 动物是哺乳动物21 and 是食肉动物23 and 有黄褐色12 and 有黑色条纹14 then 动物是虎')
            real = rule10
        elif animal == 27:
            print(features[animal - 1],
                  '\nif动物是有蹄类动物24  and 有长脖子15 and 有长腿16 and 有暗斑点13 then 动物是长颈鹿')
            real = rule11
        elif animal == 28:
            print(features[animal - 1], '\nif 动物是有蹄类动物24 and 有黑色条纹15 then 动物是斑马')
            real = rule12
        elif animal == 29:
            print(features[animal - 1],
                  '\nif 动物是鸟22 and 不会飞17 and 有长脖子16 and 有长腿15 and 有黑白二色19 then 动物是鸵鸟29')
            real = rule13
        elif animal == 30:
            print(features[animal - 1],
                  '\nif 动物是鸟22 and 不会飞17 and 会游泳18 and 有黑白二色19 then  动物是企鹅30')
            real = rule14
        elif animal == 31:
            print(features[animal - 1], '\nif 动物是鸟22 and善飞4 and 会生蛋5 then 动物是信天翁31')
            real = rule15
        key = 0
        for i in real:
            if i in new_answer:
                continue
            elif i == 21:
                if set(rule1) <= set(new_answer):
                    print("if 动物有毛发2  then  动物是哺乳动物21")
                    continue
                elif set(rule2) <= set(new_answer):
                    print("if 动物有奶1  then  动物是哺乳动物21")
                    continue
            elif i == 22:
                if set(rule3) <= set(new_answer):
                    print("if 动物有羽毛3  then  动物是鸟22")
                    continue
                elif set(rule4) <= set(new_answer):
                    print("if 动物会飞4 and  会生蛋5 then  动物是鸟22")
                    continue
            elif i == 23:
                if set(rule5) <= set(new_answer):
                    print("if 动物吃肉6 then 动物是食肉动物23")
                    continue
                elif set(rule6) <= set(new_answer):
                    print("if 动物有犀利牙齿7 and 有爪8 and 眼向前方9 then 动物是食肉动物23")
                    continue
            elif i == 24:
                if set(rule7) <= set(new_answer):
                    print("if 动物是哺乳动物21 and 有蹄10 then 动物是有蹄类动物24")
                    continue
                elif set(rule8) <= set(new_answer):
                    print("if 动物是哺乳动物21 and 反刍11 then 动物是有蹄类动物24")
                    continue
            else:
                key = 1
        if key == 0:
            print("推导成功！")
        elif key == 1:
            print("推导失败！")